package com.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.food.configure.Result;
import com.food.entity.BillExportEntity;
import com.food.entity.SaleLog;
import com.food.entity.SaleLog;
import com.food.entity.StatisticEntity;
import com.food.mapper.SaleLogMapper;
import com.food.mapper.SaleLogMapper;
import com.food.service.SaleLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.food.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2021-04-04
 */
@Service
public class SaleLogServiceImpl extends ServiceImpl<SaleLogMapper, SaleLog> implements SaleLogService {

    @Resource
    SaleLogMapper saleLogMapper;

    @Override
    public Result<SaleLog> updateSaleLog(SaleLog saleLog) {
        Result<SaleLog> result = new Result<>();
        try {
            if (StringUtils.isEmpty(saleLog.getSaleLogId())) {
                //如果ID为空，代表添加
                saleLog.setSaleLogId(IDGenerator.getUUID("SLOG"));
                saleLog.setCreateTime(new Date());
                saleLogMapper.insert(saleLog);
            } else {
                //如果ID不为空，代表修改
                saleLogMapper.updateById(saleLog);
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<SaleLog> delSaleLog(SaleLog saleLog) {
        Result<SaleLog> result = new Result<>();
        try {
            if (StringUtils.isEmpty(saleLog.getSaleLogId())) {
                result.setMessage("saleLogId can't be empty");
                return result;
            }
            saleLogMapper.deleteById(saleLog.getSaleLogId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<SaleLog> batchDelSaleLog(SaleLog saleLog) {
        Result<SaleLog> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(saleLog.getIds())) {
                String[] ids = saleLog.getIds().split(",");
                for (String id : ids) {
                    saleLogMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<SaleLog> querySaleLog(SaleLog saleLog) {
        Result<SaleLog> result = new Result<>();
        try {
            QueryWrapper<SaleLog> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用

            List<SaleLog> saleLogs = saleLogMapper.selectList(wrapper);
            result.setReturnList(saleLogs);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<SaleLog> querySaleLogByPage(SaleLog saleLog) {
        Result<SaleLog> result = new Result<>();

        try {
            if (saleLog.getCurrent() == null || saleLog.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            QueryWrapper<SaleLog> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            wrapper.lambda().orderByDesc(SaleLog::getCreateTime);
            wrapper.lambda().eq(SaleLog::getUserType,"1");

            IPage<SaleLog> saleLogs = saleLogMapper.selectPage(new Page<>(saleLog.getCurrent(), saleLog.getSize()), wrapper);
            result.setReturnPage(saleLogs);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<SaleLog> querySaleLogById(SaleLog saleLog) {
        Result<SaleLog> result = new Result<>();
        try {
            SaleLog returnSaleLog = saleLogMapper.selectById(saleLog.getSaleLogId());
            result.setReturnObj(returnSaleLog);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 账单查询
     * @param exportEntity
     * @return
     */
    @Override
    public Result<BillExportEntity> billExport(BillExportEntity exportEntity) {
        Result<BillExportEntity> result = new Result<>();
        try {
            Page<BillExportEntity> billExportEntities = saleLogMapper.billExport(new Page<>(exportEntity.getCurrent(), exportEntity.getSize()), exportEntity);
            result.setReturnPage(billExportEntities);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<StatisticEntity> materialStatistic(StatisticEntity statisticEntity) {
        Result<StatisticEntity> result = new Result<>();
        try {
            Page<StatisticEntity> statisticEntityList = saleLogMapper.materialStatistic(new Page<>(statisticEntity.getCurrent(), statisticEntity.getSize()), statisticEntity);
            result.setReturnPage(statisticEntityList);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
